//
//  QRCodeScanOverlayerDiscover.m
//  Staff
//
//  Created by jing zhao on 3/31/16.
//
//

#import "QRCodeScanOverlayerDiscover.h"
#import "NotificationDefine.h"
// 热点栏高度
#define HOTSPOT_STATUSBAR_HEIGHT            20

@interface QRCodeScanOverlayerDiscover ()

@property (strong, nonatomic) IBOutlet UIView *viewBottomBar;
@end

@implementation QRCodeScanOverlayerDiscover

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self = [[NSBundle mainBundle] loadNibNamed:@"QRCodeScanOverlayerDiscover" owner:self options:nil].lastObject;
    }
    return self;
}


- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(statusBarNofity) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2);
    CGAffineTransform transform2 = CGAffineTransformMakeRotation(M_PI);
    CGAffineTransform transform3 = CGAffineTransformMakeRotation(-M_PI/2);
    topRight.transform = transform;
    bottemRight.transform = transform2;
    bottemLeft.transform = transform3;
    
    scanView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-scanView.frame.size.width)/2, scanView.frame.origin.y, scanView.frame.size.width, scanView.frame.size.height);
    
    CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width;
    
    UIView *navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 20, viewWidth, 44)];
    navBar.backgroundColor = [UIColor colorWithRed:242/255.0 green:102/255.0 blue:71/255.0 alpha:1];
    [self addSubview:navBar];
    
    UILabel *titleTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width-200)/2,5,200,34)];
    titleTextLabel.backgroundColor = [UIColor clearColor];
    titleTextLabel.textColor = [UIColor whiteColor];
    titleTextLabel.textAlignment = NSTextAlignmentCenter;
    titleTextLabel.font = [UIFont systemFontOfSize:18];
    titleTextLabel.text = @"Scan";
    [navBar addSubview:titleTextLabel];
    
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    closeButton.backgroundColor = [UIColor clearColor];
    [closeButton setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
    closeButton.frame = CGRectMake(5, 0, 44, 44);
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        closeButton.frame = CGRectMake(5, 5, 34, 34);
    }
    
    [closeButton addTarget:self action:@selector(closeNofity) forControlEvents:UIControlEventTouchUpInside];
    [navBar addSubview:closeButton];
    
    //botombar
    float viewScreenWidth = [UIScreen mainScreen].bounds.size.width;
    UIView *bottomBar = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-76, viewScreenWidth, 76)];
    [bottomBar setBackgroundColor:[UIColor colorWithRed:242/255.0 green:102/255.0 blue:71/255.0 alpha:1]];
    self.viewBottomBar = bottomBar;
    [self addSubview:self.viewBottomBar];
    //Deacctive
    UIImageView *imageDeactive = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"QR_logo"]];
    imageDeactive.frame = CGRectMake(0, 0, 50, 45);
    imageDeactive.center = CGPointMake(72.0+25.0, 24.0);
    imageDeactive.contentMode=UIViewContentModeScaleAspectFit;
    UILabel *labelDeactive = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100.0,21.0)];
    labelDeactive.backgroundColor = [UIColor clearColor];
    labelDeactive.textColor = [UIColor whiteColor];
    labelDeactive.textAlignment = NSTextAlignmentCenter;
    labelDeactive.font = [UIFont systemFontOfSize:16];
    labelDeactive.text = @"QR code";
    labelDeactive.center=CGPointMake(72.0+25.0,48+12.0);
    [_viewBottomBar addSubview:imageDeactive];
    [_viewBottomBar addSubview:labelDeactive];
    //Discovery
    UIImageView *imageDiscovery= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ar"]];
    imageDiscovery.frame = CGRectMake(0, 0, 50, 45);
    imageDiscovery.center = CGPointMake(viewScreenWidth-72.0-25, 24.0);
    UILabel *labelDiscovery = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100.0,21.0)];
    labelDiscovery.backgroundColor = [UIColor clearColor];
    labelDiscovery.textColor = [UIColor whiteColor];
    labelDiscovery.textAlignment = NSTextAlignmentCenter;
    labelDiscovery.font = [UIFont systemFontOfSize:16];
    labelDiscovery.text = @"Discovery";
    labelDiscovery.center=CGPointMake(viewScreenWidth-72.0-25,48+12.0);
    [_viewBottomBar addSubview:imageDiscovery];
    [_viewBottomBar addSubview:labelDiscovery];
    imageDiscovery.alpha=0.5;
    labelDiscovery.alpha = 0.5;
    //buttonType
    UIButton *buttonType = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonType.frame = CGRectMake(viewScreenWidth/2.0, 0, viewScreenWidth/2.0, 76);
    [buttonType setBackgroundColor:[UIColor clearColor]];
    [buttonType addTarget:self action:@selector(tapCheckAr:) forControlEvents:UIControlEventTouchUpInside];
    [bottomBar addSubview:buttonType];
    [self setStatusBarFrame];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


-(void)setStatusBarFrame{
    float viewScreenWidth = [UIScreen mainScreen].bounds.size.width;
    CGRect frame = [[UIApplication sharedApplication]statusBarFrame];
    NSLog(@"sharedApplication %@",[NSValue valueWithCGRect:frame]);
    if ([[[UIDevice currentDevice]systemVersion]doubleValue]>7.0 && frame.size.height>30.0 ){
        self.viewBottomBar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-76-HOTSPOT_STATUSBAR_HEIGHT, viewScreenWidth, 76);
    }
    else{
        self.viewBottomBar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-76, viewScreenWidth, 76);
    }
}

-(void)statusBarNofity {
    [self setStatusBarFrame];
}



-(void)closeNofity{
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationScanCancel object:nil];
}
- (IBAction)tapCheckAr:(id)sender {
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationScanCheck object:nil];
}
@end
